//
//  CZMainController.swift
//  wuJunQianWeiBo
//
//  Created by apple on 15/12/16.
//  Copyright © 2015年 wujunqian. All rights reserved.
//

import UIKit

class CZMainController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 自定义tabbar,是只读的,不能直接使用 = 进行赋值
        // KVC 进行替换
        let mainTabBar = CZMainTabBar()
        setValue(mainTabBar, forKey: "tabBar")
        
        //创建子控制器
        //首页
        let homeVC = CZHomeController()
        self.addChildControllers(homeVC, title: "首页", imageName: "tabbar_home")
        
        //消息
        let messageVC = CZMessageController()
        self.addChildControllers(messageVC, title: "消息", imageName: "tabbar_message_center")
        
        //发现
        let discoverVC = CZDiscoverController()
        self.addChildControllers(discoverVC, title: "发现", imageName: "tabbar_discover")
        
        //我
        let profileVC = CZProfileController()
        self.addChildControllers(profileVC, title: "我", imageName: "tabbar_profile")
    }
    
    //自定义函数
    private func addChildControllers(controller:UIViewController,title:String,imageName:String)
    {
        //添加子控制器
        self.addChildViewController(UINavigationController(rootViewController: controller))
        //设置title
        controller.title = title
        //设置图片
        controller.tabBarItem.image = UIImage(named: imageName)
        //高亮状态的图片
        let highLightName = imageName + "_highlighted"
        //要取消系统的渲染
        controller.tabBarItem.selectedImage = UIImage(named: highLightName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        //要解决图片和文字的颜色不一样
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Selected)
    }
}
